import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-06-16
 * Time: 19:29
 */
//泛型的上界
class MyArray2<T extends Comparable>{
    public T findMaxVal(T[] nums){
        T max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if(nums[i].compareTo(max) > 0){
                max = nums[i];
            }
        }
        return max;
    }
    //普通泛型方法
    public <T extends Comparable> T findMaxVal3(T[] nums){
        T max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if(nums[i].compareTo(max) > 0){
                max = nums[i];
            }
        }
        return max;
    }
    //静态泛型方法
    public static <T extends Comparable> T findMaxVal2(T[] nums){
        T max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if(nums[i].compareTo(max) > 0){
                max = nums[i];
            }
        }
        return max;
    }
//    public static T findMaxVal1(T[] nums){
//        T max = nums[0];
//        for (int i = 0; i < nums.length; i++) {
//            if(nums[i].compareTo(max) > 0){
//                max = nums[i];
//            }
//        }
//        return max;
//    }
}
class MyArray3{
    //普通泛型方法
    public <T extends Comparable> T findMaxVal(T[] nums){
        T max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if(nums[i].compareTo(max) > 0){
                max = nums[i];
            }
        }
        return max;
    }
}
class MyArray4{

    //静态泛型方法
    public static <T extends Comparable> T findMaxVal(T[] nums){
        T max = nums[0];
        for (int i = 0; i < nums.length; i++) {
            if(nums[i].compareTo(max) > 0){
                max = nums[i];
            }
        }
        return max;
    }
}
class MyArray<T>{
    public T[] array;

    public MyArray(Class<T> clazz,int capacity){
        array = (T[]) Array.newInstance(clazz,capacity);
    }
    public void setVal(int pos,T val){
        array[pos] = val;
    }
    public T getVal(int pos){
        return array[pos];
    }
    public T[] getArray(){
        return array;
    }
}
public class TestDemo3 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<>(Integer.class,3);
        myArray.setVal(0,10);
        myArray.setVal(1,20);
        int tmp1 = myArray.getVal(1);
        System.out.println(tmp1);
        Integer[] tmp2 = myArray.getArray();
        System.out.println(tmp2[0]);

        MyArray<String> myArray1 = new MyArray<>(String.class,3);
        myArray1.setVal(0,"hello");
        myArray1.setVal(1,"world");
        String str = myArray1.getVal(0);
        System.out.println(str);

    }
}
